package com.a2020111222.test.proxy;

import com.a2020111222.test.aspect.userAspect;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2022/11/6 18:06
 */
public class userCglibProxy implements MethodInterceptor {
    public Object createProxy(Object target){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
//        切面对象
        userAspect userAspect = new userAspect();
//        前增强
        userAspect.checkPermissions();
//        目标类调用方法，传入参数
        Object obj = methodProxy.invokeSuper(proxy,args);
//        后增强
        userAspect.log();
        return obj;
    }
}
